{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index\n",
    "%pip install llama-index-vector-stores-awsdocdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymongo\n",
    "from llama_index.vector_stores.awsdocdb import AWSDocDbVectorStore\n",
    "from llama_index.core import VectorStoreIndex\n",
    "from llama_index.core import StorageContext\n",
    "from llama_index.core import SimpleDirectoryReader\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir -p 'data/10k/'\n",
    "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf' -O 'data/10k/uber_2021.pdf'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mongo_uri = os.environ[\"MONGO_URI\"]\n",
    "mongodb_client = pymongo.MongoClient(mongo_uri)\n",
    "store = AWSDocDbVectorStore(mongodb_client)\n",
    "storage_context = StorageContext.from_defaults(vector_store=store)\n",
    "uber_docs = SimpleDirectoryReader(\n",
    "    input_files=[\"./data/10k/uber_2021.pdf\"]\n",
    ").load_data()\n",
    "index = VectorStoreIndex.from_documents(\n",
    "    uber_docs, storage_context=storage_context\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = index.as_query_engine().query(\"What was Uber's revenue?\")\n",
    "display(f\"{response}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import Response\n",
    "\n",
    "print(store._collection.count_documents({}))\n",
    "typed_response = (\n",
    "    response if isinstance(response, Response) else response.get_response()\n",
    ")\n",
    "ref_doc_id = typed_response.source_nodes[0].node.ref_doc_id\n",
    "print(store._collection.count_documents({\"metadata.ref_doc_id\": ref_doc_id}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test delete\n",
    "if ref_doc_id:\n",
    "    store.delete(ref_doc_id)\n",
    "    print(store._collection.count_documents({}))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
